﻿using System;
using MosCraft.Utilities;

namespace MosCraft.Terrain.WorldGen
{
    class WorldGenMinable : WorldGenerator
    {
        private int numberOfBlocks;
        private int minableBlockId;

        public WorldGenMinable(int BlockID, int Num)
        {
            this.minableBlockId = BlockID;
            this.numberOfBlocks = Num;
        }

        override public bool generate(World world, FastRandom random, int x, int y, int z)
        {
            float f = random.nextFloat() * 3.141593F;
            double d = (float)(x + 8) + (QuickMath.Sin(f) * (float)numberOfBlocks) / 8F;
            double d1 = (float)(x + 8) - (QuickMath.Sin(f) * (float)numberOfBlocks) / 8F;
            double d2 = (float)(z + 8) + (QuickMath.Cos(f) * (float)numberOfBlocks) / 8F;
            double d3 = (float)(z + 8) - (QuickMath.Cos(f) * (float)numberOfBlocks) / 8F;

            double d4 = y + random.nextInt(3) + 2;
            double d5 = y + random.nextInt(3) + 2;

            for (int num = 0; num <= numberOfBlocks; num++)
            {
                double d6 = d + ((d1 - d) * (double)num) / (double)numberOfBlocks;
                double d7 = d4 + ((d5 - d4) * (double)num) / (double)numberOfBlocks;
                double d8 = d2 + ((d3 - d2) * (double)num) / (double)numberOfBlocks;
                double d9 = (random.nextDouble() * (double)numberOfBlocks) / 16D;
                double d10 = (Math.Sin(((float)num * 3.141593F) / (float)numberOfBlocks) + 1.0F) * d9 + 1.0D;
                double d11 = (Math.Sin(((float)num * 3.141593F) / (float)numberOfBlocks) + 1.0F) * d9 + 1.0D;

                int i1 = (int)Math.Floor(d6 - d10 / 2D);
                int j1 = (int)Math.Floor(d7 - d11 / 2D);
                int k1 = (int)Math.Floor(d8 - d10 / 2D);
                int l1 = (int)Math.Floor(d6 + d10 / 2D);
                int i2 = (int)Math.Floor(d7 + d11 / 2D);
                int j2 = (int)Math.Floor(d8 + d10 / 2D);

                for (int _x = i1; _x <= l1; _x++)
                {
                    double d12 = (((double)_x + 0.5D) - d6) / (d10 / 2D);

                    if (d12 * d12 >= 1.0D)
                    {
                        continue;
                    }
                    for (int _y = j1; _y <= i2; _y++)
                    {
                        double d13 = (((double)_y + 0.5D) - d7) / (d11 / 2D);

                        if (d12 * d12 + d13 * d13 >= 1.0D)
                        {
                            continue;
                        }
                        for (int _z = k1; _z <= j2; _z++)
                        {
                            double d14 = (((double)_z + 0.5D) - d8) / (d10 / 2D);

                            if (d12 * d12 + d13 * d13 + d14 * d14 < 1.0D && world.getBlockID(_x, _y, _z) == Block.Stone.blockID)
                            {
                                world.setBlockID(_x, _y, _z, minableBlockId);
                            }
                        }

                    }

                }

            }

            return true;
        }
    }
}
